<?Php

class mbackend extends CI_Model {

   
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function get_data($par1="",$par2="",$p3=""){
		$where=" WHERE 1=1";
		$user_id=$this->input->post("user_id");
		
		if($user_id){
			$where .=" AND A.tbl_user_id='".$user_id."'";
		}
		
		switch ($par1){
			case "tbl_produk_user":
				if($par2!=''){
					$where .=" AND A.id='".$par2."'";
				}
				$sql="SELECT A.id,A.nama_produk,A.deskripsi,format(A.harga,2)as harga_na,A.harga,A.status_aktif,A.persetujuan,A.tbl_user_id,
						B.nama_lengkap,E.nama_toko,A.cl_warna_id,D.warna,A.cl_kategori_id,C.kategori,A.tgl_buat,A.foto_1,A.foto_2,A.foto_3
						FROM tbl_produk_user AS A
						LEFT JOIN tbl_user AS B ON A.tbl_user_id = B.user_id
						LEFT JOIN cl_kategori AS C ON A.cl_kategori_id = C.id
						LEFT JOIN cl_warna AS D ON A.cl_warna_id = D.id
						LEFT JOIN tbl_toko_user E on A.tbl_toko_user_id=E.id ".$where;
				if($p3=='edit') return $this->db->query($sql)->row();
			break;
			
			case "tbl_toko_user":
				if($par2!=='all'){
					$sql="SELECT * FROM tbl_toko_user where tbl_user_id='".$par2."'";
					$data=array();
					$data["toko_user"]=$this->db->query($sql)->row();
					$data["jmlna"]=count($this->db->query($sql)->result_array());
					return $data;
				}
				else{$sql="SELECT A.*,B.nama_lengkap FROM tbl_toko_user A LEFT JOIN tbl_user B ON A.tbl_user_id=B.user_id ";}
				
			break;
			case "list_produk":
				$id_toko=$this->input->post('id');
				$where=" WHERE 1=1 ";
				if($id_toko){
					$where .=" AND E.id='".$id_toko."'";
				}
				$sql="SELECT A.id,A.nama_produk,A.deskripsi,format(A.harga,2)as harga_na,A.harga,A.status_aktif,A.persetujuan,A.tbl_user_id,
						B.nama_lengkap,E.nama_toko,A.cl_warna_id,D.warna,A.cl_kategori_id,C.kategori,A.tgl_buat,
						A.foto_1,A.foto_2,A.foto_3,A.rating
						FROM tbl_produk_user AS A
						LEFT JOIN tbl_user AS B ON A.tbl_user_id = B.user_id
						LEFT JOIN cl_kategori AS C ON A.cl_kategori_id = C.id
						LEFT JOIN cl_warna AS D ON A.cl_warna_id = D.id
						LEFT JOIN tbl_toko_user E on A.tbl_toko_user_id=E.id ".$where;
				//return $this->db->query($sql)->row();
			break;
			
			case "tbl_toko_user_data":
				$sql="SELECT * FROM tbl_toko_user where tbl_user_id='".$p3."'";
				
			break;
			
			case "cl_warna":
			case "tbl_faq":
			case "cl_kategori":
				$where =' WHERE 1=1 ';
				if($par2!==''){
					$where .=' AND id='.$par2;
				}
				$sql="SELECT * FROM ".$par1.$where;
				if($par2!==''){
					return $this->db->query($sql)->row();
				}
			break;
			case "tbl_portofolio":
				$where =' WHERE 1=1 ';
				if($par2!==''){
					$where .=' AND id='.$par2;
				}
				$sql="SELECT * FROM tbl_porto_folio ".$where;
				if($par2!==''){
					return $this->db->query($sql)->row();
				}
			break;
			
			case "tbl_narsis":
				$where =' WHERE 1=1 ';
				if($par2!==''){
					$where .=' AND id='.$par2;
				}
				$sql="SELECT * FROM tbl_narsis ".$where;
				if($par2!==''){
					return $this->db->query($sql)->row();
				}
			break;
			
			
			
			
			case "tbl_pesan_email":
				if($par2!=='all'){
					$sql="SELECT * FROM tbl_pesan_email where email ='".$par2."'";
					return $this->db->query($sql)->result_array();
				}
				else{
					$sql="SELECT A.*,B.nama_produk,B.foto_1 FROM tbl_pesan_email A
							LEFT JOIN tbl_produk_user B ON A.tbl_produk_id=B.id order by A.id DESC";
					return $this->get_jsonna($sql);
				}
				
				
			break;
			
			case "tbl_gambar_produk":
				$sql="SELECT * FROM tbl_gambar_produk where tbl_produk_id='".$par2."' order by id";
				$data=$this->db->query($sql)->result_array();
				return $data;
			break;

			case "tbl_user":
				if($par2!=='all'){
					$sql="SELECT * FROM tbl_user where user_id='".$par2."'";
				}
				else{
					$sql="SELECT A.*,B.nama_group FROM tbl_user A LEFT JOIN cl_user_group B ON A.group_user_id=B.id ";
					return $this->get_jsonna($sql);
				}
				$data=$this->db->query($sql)->row();
				return $data;
			break;
			case "foto":
				if($par2!==''){
					$sql="SELECT * FROM tbl_user where user_id='".$par2."'";
				}
				return $this->db->query($sql)->row("foto");
			break;
		
		}
		//$res=$this->db->query($sql)->result_array();
		return $this->get_jsonna($sql);
		//return $res;
		
	}
	
	function get_jsonna($sql){
		$page = (integer) (($this->input->post('page')) ? $this->input->post('page') : "1");
		$limit = (integer) (($this->input->post('rows')) ? $this->input->post('rows') : "15");
		 
		$count = $this->db->query($sql)->num_rows();
		
		if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages; 
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if($start<0) $start=0;
		  
		$sql = $sql . " LIMIT $start,$limit";
		
		 $data=$this->db->query($sql)->result();  
			if($total_pages==0){
				$kos='{"total":0,"rows":[]}';
				return $kos;
			}
			else{  	
				if($data){
				   $responce->rows= $data;
				   $responce->total =$count;
				   return json_encode($responce);
				}else{ 
				   return "";
				}   
			}
 	}
	
	function simpan($table="",$post=""){
		//$CI=&get_instance();
		$this->load->library('encrypt');
		$this->db->trans_begin();
		$data=array();
		$sts=$post["sts"];
		$id=$post["id"];
		//echo $sts;exit;
		unset ($post["sts"]);
		unset ($post["id"]);
		
		switch($table){
			case "tbl_toko_user":
			case "cl_kategori":
			case "cl_warna":
			case "tbl_faq":
				if($sts=='edit'){
					$this->db->where('id',$id);	
				}
			break;
			case "tbl_porto_folio":
				
				if($sts=='edit'){
					$gbr=$this->db->get_where($table,array('id'=>$id))->row('gambar_porto_folio');
					chmod('repositori/porto_folio/'.$gbr,0777);
					unlink('repositori/porto_folio/'.$gbr);
					$this->db->where('id',$id);	
				}
				
				
			break;
			case "tbl_narsis":
				
				if($sts=='edit'){
					$gbr=$this->db->get_where($table,array('id'=>$id))->row('gambar_narsis');
					chmod('repositori/narsis/'.$gbr,0777);
					unlink('repositori/narsis/'.$gbr);
					$this->db->where('id',$id);	
				}
				
				
			break;
			

			
			case "tbl_user":
				if($sts=='edit'){
					$this->db->where('user_id',$id);	
				}
				else{
					$post["password"] = $this->encrypt->encode($post["password"]);
					$post["status_user"]=1;
					$post["group_user_id"]=2;
				}
				
			break;
			
			case "tbl_produk_user":
				$harga			= str_replace(",", "", $this->input->post('harga'));
		
				unset($post["harga"]);
				$post["harga"]=$harga;
				if($sts=='edit'){
					$this->db->where('id',$id);	
				}
				else{
					$post["status_aktif"]=2;
					$post["persetujuan"]=2;
					$post["rating"]=0;
				}
			break;
			case "tbl_pesan_email":
				if($sts=='edit'){
					$this->db->where('id',$id);	
				}
				else{$post["flag"]=1;}
			break;
			

		}
		
		if($sts=='add'){
			$post["tgl_buat"]=date('Y-m-d');
			if($table=='tbl_toko_user' || $table=='tbl_produk_user')$post["tbl_user_id"]=$this->sesina["user_id"];
			$this->db->insert($table,$post);
			//echo $this->db->last_query();exit;
			if($table=='tbl_toko_user')$data["id"]= $this->db->insert_id();
			//if($table=='tbl_produk_user')return $this->db->insert_id();
		}
		
		else{
			$post["update_date"]=date('Y-m-d');
			//print_r($post);exit;
			$this->db->update($table,$post);
			//echo $this->db->last_query();exit;
		}
		
		if($this->db->trans_status() == false){
				$this->db->trans_rollback();
				if($table=='tbl_toko_user'){
					$data["msg"]=2;
					$data["id"]="";
					echo json_encode($data);exit;
				}
				else{
					return "Data not saved";
				}
		} else{
				if($table=='tbl_toko_user'){
					$this->db->trans_commit();
					$data["msg"]=1;
					$data["id"]="";
					echo json_encode($data);exit;
				}
				else{
					return $this->db->trans_commit();
				}
		}
	}
	
	function isi_combo($table,$field_id,$field_display){
		$sql="select ".$field_id." as id ,".$field_display." as text from ".$table;
		return $this->db->query($sql)->result_array();
	}
	
	function hapus($table){
		$this->db->trans_begin();
		$id=$this->input->post("id");
		switch($table){
			case "tbl_produk_user":
				$sql="delete from tbl_produk_user where id='".$id."'";
				deleteDirectory('repositori/produk_user/'.$this->input->post('user'));
			break;
			
			case "tbl_user":
				$sql="delete from tbl_produk_user where tbl_user_id='".$id."'";
				$this->db->query($sql);
				deleteDirectory('repositori/produk_user/'.$id);
				$sql="delete from tbl_toko_user where tbl_user_id='".$id."'";
				$this->db->query($sql);
				deleteDirectory('repositori/logo_toko/'.$id);
				$sql="delete from tbl_user where user_id='".$id."'";
				//$this->db->query($sql);
			break;
			case "tbl_toko_user":
			
				$sql="delete from tbl_produk_user where tbl_toko_user_id='".$id."'";
				$this->db->query($sql);
				deleteDirectory('repositori/produk_user/'.$this->input->post('user'));
				deleteDirectory('repositori/logo_toko/'.$this->input->post('user'));
				$sql="delete from tbl_toko_user where id='".$id."'";
			break;
			
			case "tbl_porto_folio":
				if($this->input->post('file')){
					chmod('repositori/porto_folio/'.$this->input->post('file'),0777);
					unlink('repositori/porto_folio/'.$this->input->post('file'));
				}
				$sql="delete from tbl_porto_folio where id='".$id."'";
				
			break;
			
			case "tbl_narsis":
				if($this->input->post('file')){
					chmod('repositori/narsis/'.$this->input->post('file'),0777);
					unlink('repositori/narsis/'.$this->input->post('file'));
				}
				$sql="delete from tbl_narsis where id='".$id."'";
			break;
			case "cl_warna":
			case "cl_kategori":
			case "tbl_faq":
				$sql="delete from ".$table." where id='".$id."'";
			break;
			case "tbl_pesan_email":
				$design=$this->input->post('design');
				$design2=$this->input->post('design2');
				if($design){
					chmod('repositori/cetak_sendiri/'.$design,0777);
					unlink('repositori/cetak_sendiri/'.$design);
				}
				if($design2){
					chmod('repositori/cetak_sendiri/'.$design2,0777);
					unlink('repositori/cetak_sendiri/'.$design2);
				}
				$sql="delete from tbl_pesan_email where id='".$id."'";
			break;
			
		}
		
		$this->db->query($sql);
		if($this->db->trans_status() == false){
		$this->db->trans_rollback();
		return "Data not saved";
		} else{
		return $this->db->trans_commit();
		}
	}
	
}